<template>
	<view class="select-tree">
		<view class="select-tree-mask" :class="{ show: showTree }" @click="_cancel"></view>
		<view class="select-tree-cnt" :class="{ show: showTree }">
			<view class="select-tree-bar">
				<view class="bar-cancel" :style="{ color: cancelColor }" @click="_cancel">取消</view>
				<view class="bar-title" :style="{ color: titleColor }">{{ title }}</view>
				<view class="bar-confirm" :style="{ color: confirmColor }" @click="_confirm">确定</view>
			</view>
			<view class="select-tree-view">
				<scroll-view class="view-sc" :scroll-y="true">
					<block v-for="(item, index) in treeList" :key="index">
						<view
							class="select-tree-item"
							:style="[{ paddingLeft: item.rank * 20 + 'px', zIndex: item.rank * -1 + 50 }]"
							:class="{ border: border == true, show: item.show, last: item.lastRank, showchild: item.showChild, open: item.open }"
						>
							<view class="select-tree-label" @click.stop="_treeItemTap(item, index)">
								<block v-if="!item.lastRank && item.showChild"><u-icon class="icon" name="arrow-down-fill" color="#757575" size="24"></u-icon></block>
								<block v-else-if="!item.lastRank"><u-icon class="icon" name="play-right-fill" color="#757575" size="24"></u-icon></block>
								{{ item.name }}
							</view>
							<view class="select-tree-check" @click.stop="_treeItemSelect(item, index)" v-if="selectParent ? true : item.lastRank">
								<view class="check-yes" v-if="item.checked" :class="{ radio: !multiple }" :style="{ 'border-color': confirmColor }">
									<view class="check-yes-b" :style="{ 'background-color': confirmColor }"></view>
								</view>
								<view class="check-no" v-else :class="{ radio: !multiple }" :style="{ 'border-color': confirmColor }"></view>
							</view>
						</view>
					</block>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'select-tree',
	props: {
		range: {
			type: Array,
			default: function() {
				return [];
			}
		},
		idKey: {
			type: String,
			default: 'id'
		},
		selected: {
			type: String,
			default: ''
		},
		rangeKey: {
			type: String,
			default: 'name'
		},
		title: {
			type: String,
			default: ''
		},
		multiple: {
			type: Boolean,
			default: true
		},
		selectParent: {
			type: Boolean,
			default: true
		},
		foldAll: {
			type: Boolean,
			default: false
		},
		confirmColor: {
			type: String,
			default: ''
		},
		cancelColor: {
			type: String,
			default: ''
		},
		titleColor: {
			type: String,
			default: ''
		},
		currentIcon: {
			type: String,
			default: ''
		},
		defaultIcon: {
			type: String,
			default: ''
		},
		lastIcon: {
			type: String,
			default: ''
		},
		border: {
			type: Boolean,
			default: false
		},
		show: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			treeList: [],
			selectIndex: -1,
			treeType: 1
		};
	},
	computed: {
		showTree: {
			get() {
				if (this.show) return this.show;
				else return false;
			},
			set() {}
		}
	},
	methods: {
		_show() {
			this.showTree = true;
		},
		_hide() {
			this.showTree = false;
		},
		_cancel() {
			this._hide();
			this.$emit('cancel', '');
		},
		_confirm() {
			let rt = [],
				obj = {};
			this.treeList.forEach((v, i) => {
				if (this.treeType == 1) {
					if (this.treeList[i].checked) {
						obj = {};
						obj.parents = this.treeList[i].parents;
						obj = Object.assign(obj, this.treeList[i].source);
						rt.push(obj);
					}
				} else {
					if (this.treeList[i].checked && v.parentId.length > 0) {
						obj = {};
						obj.parents = this.treeList[i].parents;
						obj = Object.assign(obj, this.treeList[i].source);
						delete obj.list;
						rt.push(obj);
					}
				}
			});

			//
			this._hide();
			this.$emit('confirm', rt);
		},
		_renderTreeList(list = [], rank = 0, parentId = [], parents = []) {
			let me = this;
			let arr = me.selected.split(',');
			list.forEach(item => {
				this.treeList.push({
					id: item[this.idKey],
					name: item[this.rangeKey],
					source: item,
					parentId,
					parents,
					rank,
					showChild: false,
					open: false,
					show: rank == 0,
					hideArr: [],
					orChecked: arr.indexOf(item[this.idKey]) != -1 ? true : false,
					checked: arr.indexOf(item[this.idKey]) != -1 ? true : false
				});
				if (Array.isArray(item.list) && item.list.length > 0) {
					this.treeType = 2;
					let parentid = [...parentId],
						parentArr = [...parents];
					delete parentArr.list;
					parentid.push(item[this.idKey]);
					parentArr.push({
						[this.idKey]: item[this.idKey],
						[this.rangeKey]: item[this.rangeKey]
					});
					this._renderTreeList(item.list, rank + 1, parentid, parentArr);
				} else {
					this.treeList[this.treeList.length - 1].lastRank = true;
				}
			});
		},
		_defaultSelect() {
			this.treeList.forEach((v, i) => {
				if (v.checked) {
					this.treeList.forEach((v2, i2) => {
						if (v.parentId.toString().indexOf(v2.parentId.toString()) >= 0) {
							v2.show = true;
							if (v.parentId.includes(v2.id)) {
								v2.showChild = true;
								v2.open = true;
							}
						}
					});
				}
			});
		},
		_treeItemTap(item, index) {
			if (item.lastRank == true) {
				this.treeList[index].checked = !this.treeList[index].checked;
				this._fixMultiple(index);
				return;
			}
			let list = this.treeList;
			let id = item.id;
			item.showChild = !item.showChild;
			item.open = item.showChild ? true : !item.open;
			list.forEach((childItem, i) => {
				if (item.showChild == false) {
					if (!childItem.parentId.includes(id)) {
						return;
					}
					if (!this.foldAll) {
						if (childItem.lastRank !== true && !childItem.open) {
							childItem.showChild = false;
						}
						if (childItem.show) {
							childItem.hideArr[item.rank] = id;
						}
					} else {
						if (childItem.lastRank !== true) {
							childItem.showChild = false;
						}
					}
					childItem.show = false;
				} else {
					if (childItem.parentId[childItem.parentId.length - 1] == id) {
						childItem.show = true;
					}
					if (childItem.parentId.includes(id) && !this.foldAll) {
						if (childItem.hideArr[item.rank] == id) {
							childItem.show = true;
							if (childItem.open && childItem.showChild) {
								childItem.showChild = true;
							} else {
								childItem.showChild = false;
							}
							childItem.hideArr[item.rank] = null;
						}
					}
				}
			});
		},
		_treeItemSelect(item, index) {
			this.treeList[index].checked = !this.treeList[index].checked;
			this._fixMultiple(index);
		},
		_fixMultiple(index) {
			if (!this.multiple) {
				this.treeList.forEach((v, i) => {
					if (i != index) {
						this.treeList[i].checked = false;
					} else {
						this.treeList[i].checked = true;
					}
				});
			} else {
				this.treeList.forEach((obj, i) => {
					if (!this.treeList[index].checked && this.treeList[index].parentId.indexOf(obj.id) > -1) {
						this.treeList[i].checked = false;
					} else if (obj.parentId.indexOf(this.treeList[index].id) > -1) {
						this.treeList[i].checked = this.treeList[index].checked;
					}
				});
			}
		},
		_reTreeList() {
			this.treeList.forEach((v, i) => {
				this.treeList[i].checked = v.orChecked;
			});
		},
		_initTree(range = this.range) {
			this.treeList = [];
			this._renderTreeList(range);
			this.$nextTick(() => {
				this._defaultSelect(range);
			});
		}
	},
	watch: {
		range(list) {
			this._initTree(list);
		},
		selected() {
			this._initTree();
			// this._renderTreeList();
		},
		multiple() {
			if (this.range.length) {
				this._reTreeList();
			}
		},
		selectParent() {
			if (this.range.length) {
				this._reTreeList();
			}
		}
	},
	mounted() {
		this._initTree();
	}
};
</script>

<style lang="scss">
.select-tree-mask {
	position: fixed;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	z-index: 9998;
	background-color: rgba(0, 0, 0, 0.6);
	opacity: 0;
	transition: all 0.3s ease;
	visibility: hidden;

	&.show {
		visibility: visible;
		opacity: 1;
	}
}
.select-tree-cnt {
	position: fixed;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	z-index: 9999;
	top: 480rpx;
	transition: all 0.3s ease;
	transform: translateY(100%);

	&.show {
		transform: translateY(0);
	}
}
.select-tree-bar {
	background-color: #fff;
	height: 90rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	box-sizing: border-box;
	border: 1rpx solid #f5f5f5;
	font-size: 16px;
	color: #888;
	line-height: 1;

	.bar-confirm {
		padding: 0 28rpx;
		color: #e5231a;
		line-height: 90rpx;
	}

	.bar-cancel {
		padding: 0 28rpx;
		color: #888;
		line-height: 90rpx;
	}
}

.select-tree-view {
	position: absolute;
	top: 0rpx;
	right: 0rpx;
	bottom: 0rpx;
	left: 0rpx;
	top: 90rpx;
	background-color: #fff;
	padding: 20rpx;

	.view-sc {
		height: 100%;
		overflow: hidden;
	}
}
.select-tree-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 16px;
	color: #757575;
	line-height: 1;
	height: 0;
	opacity: 0;
	transition: 0.2s;
	position: relative;
	overflow: hidden;

	&.show {
		height: 80rpx;
		opacity: 1;
	}

	&.showchild:before {
		transform: rotate(90deg);
	}

	&.last:before {
		opacity: 0;
	}
}
.select-tree-icon {
	width: 26rpx;
	height: 26rpx;
	margin-right: 8rpx;
}
.select-tree-label {
	flex: 1;
	display: block;
	font-size: 16px;
	line-height: 1;
	text-align: left;
	overflow: hidden;
	text-overflow: ellipsis;
	word-break: keep-all;

	.icon {
		margin-right: 16rpx;
	}
}
.select-tree-check {
	width: 40px;
	height: 40px;
	display: flex;
	justify-content: center;
	align-items: center;

	.check-yes,
	.check-no {
		width: 20px;
		height: 20px;
		border-radius: 20%;
		border: 1rpx solid #e5231a;
		display: flex;
		justify-content: center;
		align-items: center;
		box-sizing: border-box;
	}
	.check-yes-b {
		width: 12px;
		height: 12px;
		border-radius: 20%;
		background-color: #e5231a;
	}
}
</style>
